<?php

  if (isset($_POST['submit0'])) { // If password entered...
    $word = $_POST['password']; // Variable set as entered password
    setcookie('textadmin', $word, time() + 31536000); // Cookie is set
  } else { // If password form filled in previously...
    if (isset($_COOKIE['textadmin'])) { // Check for cookie
      $word = $_COOKIE['textadmin']; // Variable set as cookie
    }
  }

  // New page
  if (isset($_POST['submit'])) { // Handle the form

    // Check for a file title
    if (strlen($_POST['create']) < 1) {
      $problem = true;
      $response1 = "You didn't enter a page name.<br />";
    }

    // Check for a page title
    if (strlen($_POST['pagetitle']) < 1) {
      $problem = true;
      $response2 = "You didn't enter a page title.<br />";
    }

    // Check for keywords
    if (strlen($_POST['keywords']) < 1) {
      $problem = true;
      $response3 = "You didn't enter any keywords.<br />";
    }

    if (!$problem) {

      // Create the file title
      $filetitle = $_POST['create'];
      $filetitle = trim(stripslashes($filetitle));

      // Create the page title
      $pagetitle = $_POST['pagetitle'];
      $pagetitle = trim(stripslashes($pagetitle));

      // Create the meta keywords
      $keywords = $_POST['keywords'];
      $keywords = trim(stripslashes($keywords));
      $keywords = str_replace(", ", "|", $keywords);

      // Build the item
      $item = "\n/$filetitle|$pagetitle|$keywords";

      // Set path to file
      $filename = "../search/index.txt";
      // Apend the contents
      $fp = fopen($filename, "a") or die("Couldn't open $filename.");
      fwrite($fp, $item);
      fclose($fp);

      // Feedback
      $response1 = "The search index was successfully updated with<br />$item";
    }

  }

?>

<?php $thisPage = "addsearch"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Add to search index</title>
<meta name="robots" content="noindex,nofollow" />
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>
<body>

<div id="wrap">

<?php

// Start password routine
  include_once('password.php');
  if ($word != $le_password) {

?>

<!-- password form //-->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<b>Enter password:</b>
<br />
<input type="password" name="password" size="25" maxlength="25" />
<br />
<input type="submit" name="submit0" value="Submit" />
</p>
</form>
<!-- password form_eof //-->

<?php

    // Routine to display password entry error message.
    if (isset($_POST['submit0'])) {
      if ($_POST['password'] != $le_password) {
        echo '<p">Incorrect password!</p>';
      }
    }

  } else {
// End password routine

?>

<?php include_once('navigation.php'); ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Page name (existing filename with no capitals or file suffix) - eg: <b>index</b> or <b>contact</b> etc<br />
<input type="text" name="create" id="create" size="40" value="<?php if (isset($_POST['submit'])) { echo stripslashes($_POST['create']); } else { echo $create; } ?>" maxlength="60" /><br />
Page title desired for search results listing<br />
<input type="text" name="pagetitle" id="pagetitle" size="40" value="<?php if (isset($_POST['submit'])) { echo stripslashes($_POST['pagetitle']); } else { echo $pagetitle; } ?>" maxlength="60" /><br />
Keywords and phrases (separate with comma+space)<br />
<textarea class="textarea" name="keywords" cols="70" rows="3"><?php if (isset($_POST['submit'])) { echo stripslashes($_POST['keywords']); } else { echo $keywords; } ?></textarea><br />
<input type="submit" name="submit" value="Submit">
</form>

<?php echo $response1 . $response2 . $response3; ?>

<h3>Existing search index</h3>

<?php

  // Set the path to the file
  $filename = "../search/index.txt";
  // Get the file contents as an array
  $fileArray = file($filename);
  // Loop through the array
  foreach ($fileArray as $val) {
    $val = str_replace("|", ", ", $val);
    echo "&#8226; $val<br />";
  }

?>

<?php

  } // Required for password routine

?>

</div>

</body>
</html>